//
//  TrastionAnimation.m
//  手势转场动画
//
//  Created by iOS on 16/7/18.
//  Copyright © 2016年 iOS. All rights reserved.
//

#import "TrastionAnimation.h"
@interface TrastionAnimation()
@property (nonatomic,strong)UIViewController * fromVC;
@property (nonatomic,strong)UIViewController * toVC;
@property (nonatomic,strong)UIView * ContainerView ;
@end
@implementation TrastionAnimation
static NSTimeInterval TrasitonTime;
static id<UIViewControllerContextTransitioning> TransitionContext;
- (instancetype)init{
    if (self = [super init]) {
        
    }
    return self;
}
- (CGFloat)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 3.0;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
// zhe一段是完全没问题的
    UIView * containerView = [transitionContext containerView];
    

    UIViewController * fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
    UIViewController * toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView * fromView = fromViewController.view;
    
    UIView * toView = toViewController.view;
    
    [containerView addSubview:toView];
    
    
//    CATransform3D transform = CATransform3DIdentity;
//    
//    transform.m34 = -0.0001;
//    
//     [containerView.layer setSublayerTransform:transform];
    
    
//    CGRect  initialFrame = [transitionContext initialFrameForViewController:fromViewController];
//    
//    
//    //设置视图大小
//    toView.frame = initialFrame;
//    fromView.frame = initialFrame;
    
    //是否是反面
    float factor = self.pushOrPop ? -1.0 : 1.0;
    
    //push操作 y轴方向顺时针旋转90度 这样第一时间是看不到toView的
    toView.layer.transform = [self yRotation:factor  *  -M_PI_2];
    
    //animate
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];
    
    [UIView animateKeyframesWithDuration:duration delay:0 options:0 animations:^{
        
        
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
            
            //fromView-----
            
           // fromView.layer.transform = [self yRotation:factor * M_PI_2];
           [fromView.layer setTransform:[self trasfrom: factor * M_PI_2]];
        }];
        
        [UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
            
             [toView.layer setTransform: [self trasfrom: 0]];
            //toVIew复原 在先前顺时针的基础上 逆时针旋转90度 复原
            //toView.layer.transform = [self yRotation:0.0];
            
        }];
        
        
    } completion:^(BOOL finished) {
        
        
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        
    }];
    
//*/
    
    
    
    
    
    
    
    
    
   /*************************/
//   self.fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
//   self.toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//    self.ContainerView = [transitionContext containerView];
//    TrasitonTime = [self transitionDuration:transitionContext];
//    TransitionContext = transitionContext;
//    self.fromVC.view.frame = [UIScreen mainScreen].bounds;
//    self.toVC.view.frame = [UIScreen mainScreen].bounds;
//    [self.ContainerView addSubview:self.toVC.view];
//    if (self.pushOrPop == YES) {
//        [self pushOrPresent];
//    }else{
//        [self popOrDismiss];
//    }
}
-(CATransform3D )yRotation:(CGFloat)angle{
    
    //    矢量(0,1,0)旋转angle角度
    return CATransform3DMakeRotation(angle, 0.0, 1.0, 0.0);
    
}

- (void)pushOrPresent{
  //  [_ContainerView addSubview:_toVC.view];
 //   _toVC.view.alpha = 0;
    [self animationTrasFromDirect:-1.0];
    }

- (void)popOrDismiss{
    
    [self animationTrasFromDirect:1.0];

}
- (void)animationTrasFromDirect:(CGFloat)directFlage{
    //这一段如果把时间改长也会出现一系列问题
    [self.toVC.view.layer setTransform:[self trasfrom: - M_PI_2 * directFlage ]];
  // _toVC.view.layer.transform = [self yRotation:directFlage  *  -M_PI_2];
    [UIView animateKeyframesWithDuration:TrasitonTime delay:0 options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0 relativeDuration:TrasitonTime/2.0 animations:^{
            [self.fromVC.view.layer setTransform:[self trasfrom:M_PI_2 * directFlage]];
        }];
        [UIView addKeyframeWithRelativeStartTime:TrasitonTime/2.0 relativeDuration:TrasitonTime/2.0  animations:^{
         
            [self.toVC.view.layer setTransform:CATransform3DIdentity];
        }];
    } completion:^(BOOL finished) {
        [TransitionContext completeTransition:![TransitionContext transitionWasCancelled]];
    }];
  //  [TransitionContext completeTransition:YES];
  
    
    /*****************/
    /*   这一段手势驱动不好用
CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _toVC.view.frame = CGRectMake(0, 0, screenSize.width, screenSize.height);
    [self.ContainerView addSubview:self.toVC.view];
    self.toVC.view.alpha = 0;
    [UIView animateWithDuration:TrasitonTime/2.0 animations:^{
        [self.ContainerView.layer setTransform:[self trasfrom:M_PI_2 * directFlage]];
    } completion:^(BOOL finished) {
        self.toVC.view.alpha = 1;
        [UIView animateWithDuration:TrasitonTime/2.0 animations:^{
            [self.ContainerView.layer setTransform:[self trasfrom:0]];
        } completion:^(BOOL finished) {
            [TransitionContext completeTransition:![TransitionContext transitionWasCancelled]];
        }];
        
    }];
  */
}
- (CATransform3D)trasfrom:(CGFloat)angle{
    CATransform3D trasFrom = CATransform3DIdentity;
    trasFrom.m34 = 1.0 /-900;
    trasFrom = CATransform3DRotate(trasFrom,angle, 0, 1, 0);
    return trasFrom;
}
@end
